package ru.spbau.bytecode.projectobserver;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import ru.spbau.bytecode.graph.CompositeVertex;
import ru.spbau.bytecode.graph.Graph;
import ru.spbau.bytecode.graph.Vertex;

public class GraphComparator {

	public boolean equals(Graph g1, Graph g2) {
		List<CompositeVertex> l1 = new LinkedList<CompositeVertex>();
		List<CompositeVertex> l2 = new LinkedList<CompositeVertex>();
		l1.addAll(g1.getVertices());
		l2.addAll(g2.getVertices());
		VertexComparator cmp = new VertexComparator();
		
		Collections.sort(l1, cmp);
		Collections.sort(l2, cmp);
		
		if (!l1.equals(l2)) {
			return false;
		}
		
		for (int i = 0; i < l1.size(); i++) {
			CompositeVertex v1 = l1.get(i);
			CompositeVertex v2 = l2.get(i);
			
			List<Vertex> list1 = v1.getInnerVertices();
			List<Vertex> list2 = v2.getInnerVertices();
			
			Collections.sort(list1, cmp);
			Collections.sort(list2, cmp);
			
			if (!list1.equals(list2)) {
				return false;
			}
		}
		
		if (!g1.getEdges().equals(g2.getEdges())) {
			System.err.println("edges");
			System.err.println(g1.getEdges());
			System.err.println(g2.getEdges());
			return false;
		}
		
		return true;
	}
	
	class VertexComparator implements Comparator<Vertex> {

		@Override
		public int compare(Vertex v1, Vertex v2) {
			return v1.getName().compareTo(v2.getName());
		}
		
	}

}
